package com.sunchangming.myweb;

import com.google.api.client.auth.oauth2.Credential;
import com.google.common.base.Preconditions;

/**
 * Persisted credential implementation to be used exclusively with
 * {@link MySQLCredentialStore}.
 * 
 * @author Yaniv Inbar
 */

class MyPersistedCredential {

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public String getRefreshToken() {
		return refreshToken;
	}

	public void setRefreshToken(String refreshToken) {
		this.refreshToken = refreshToken;
	}

	public Long getExpirationTimeMillis() {
		return expirationTimeMillis;
	}

	public void setExpirationTimeMillis(Long expirationTimeMillis) {
		this.expirationTimeMillis = expirationTimeMillis;
	}

	/** User ID to be used as the primary key. */
	private String userId;

	/** Access token or {@code null} for none. */
	private String accessToken;

	/** Refresh token {@code null} for none. */
	private String refreshToken;

	/** Expiration time in milliseconds {@code null} for none. */

	private Long expirationTimeMillis;

	/**
	 * @param userId
	 *            user ID whose credential needs to be stored
	 * @param credential
	 *            credential whose {@link Credential#getAccessToken access
	 *            token}, {@link Credential#getRefreshToken refresh token}, and
	 *            {@link Credential#getExpirationTimeMilliseconds expiration
	 *            time} need to be stored
	 */
	MyPersistedCredential(String userId, Credential credential) {
		this.userId = Preconditions.checkNotNull(userId);
		accessToken = credential.getAccessToken();
		refreshToken = credential.getRefreshToken();
		expirationTimeMillis = credential.getExpirationTimeMilliseconds();
	}

	/**
	 * @param credential
	 *            credential whose {@link Credential#setAccessToken access
	 *            token}, {@link Credential#setRefreshToken refresh token}, and
	 *            {@link Credential#setExpirationTimeMilliseconds expiration
	 *            time} need to be set if the credential already exists in
	 *            storage
	 */
	void load(Credential credential) {
		credential.setAccessToken(accessToken);
		credential.setRefreshToken(refreshToken);
		credential.setExpirationTimeMilliseconds(expirationTimeMillis);
	}
}
